#!/bin/bash

# 代码质量检查脚本
# 用于检查项目代码是否符合开发规范

set -e

echo "🔍 开始代码质量检查..."

# 检查后端代码
echo "📝 检查后端代码..."
cd backend

# 检查PHP语法
echo "  - 检查PHP语法..."
find app -name "*.php" -exec php -l {} \; | grep -v "No syntax errors"

# 运行PHP CS Fixer检查
if command -v php-cs-fixer &> /dev/null; then
    echo "  - 运行PHP CS Fixer检查..."
    php-cs-fixer fix --dry-run --diff
else
    echo "  ⚠️  PHP CS Fixer未安装，跳过检查"
fi

# 运行PHPUnit测试
if [ -f "vendor/bin/phpunit" ]; then
    echo "  - 运行PHPUnit测试..."
    vendor/bin/phpunit --testdox
else
    echo "  ⚠️  PHPUnit未安装，跳过测试"
fi

cd ..

# 检查前端代码
echo "🎨 检查前端代码..."
cd frontend

# 检查依赖
if [ -f "package-lock.json" ]; then
    echo "  - 检查依赖完整性..."
    npm ci --only=production
fi

# 运行ESLint检查
if [ -f "node_modules/.bin/eslint" ]; then
    echo "  - 运行ESLint检查..."
    npm run lint
else
    echo "  ⚠️  ESLint未安装，跳过检查"
fi

# 运行TypeScript类型检查
if [ -f "node_modules/.bin/vue-tsc" ]; then
    echo "  - 运行TypeScript类型检查..."
    npm run type-check
else
    echo "  ⚠️  TypeScript编译器未安装，跳过检查"
fi

# 运行测试
if [ -f "node_modules/.bin/vitest" ]; then
    echo "  - 运行测试..."
    npm run test
else
    echo "  ⚠️  Vitest未安装，跳过测试"
fi

cd ..

echo "✅ 代码质量检查完成！"

# 检查结果汇总
echo ""
echo "📊 检查结果汇总："
echo "  - 后端PHP代码: ✅ 完成"
echo "  - 前端Vue代码: ✅ 完成"
echo "  - 代码规范: ✅ 完成"
echo "  - 测试覆盖: ✅ 完成"
echo ""
echo "💡 提示：如果发现问题，请根据错误信息进行修复"
